package com.apiserver.data.mybatis.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MapTools {


    /**
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Author feizhou
     * @Description 将Object对象里面的属性和值转化成Map对象
     * @Date \ 2019/10/9 0009
     * @Param [obj]
     **/

    public static Map<String, Object> objectToMap(Object obj) {


        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        try {
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                String fieldName = field.getName();
                Object value = null;


                value = field.get(obj);
                map.put(fieldName, value);
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Author feizhou
     * @Description 将Object对象里面的属性和值转化成Map对象
     * @Date \ 2019/10/9 0009
     * @Param [obj]
     **/
    public static Map<String, Object> objectToMapNotNull(Object obj) {


        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        try {
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                String fieldName = field.getName();
                Object value = null;


                value = field.get(obj);
                if (value != null) {
                    map.put(fieldName, value);
                }

            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static void main(String[] args) {

    }

}